package org.eiahe.hr.salary.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.utils.MapstructUtils;
import org.ehe.common.core.utils.StringUtils;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.system.domain.vo.SysDeptVo;
import org.ehe.system.service.ISysDeptService;
import org.eiahe.hr.salary.domain.SalaryHistory;
import org.eiahe.hr.salary.domain.bo.SalaryHistoryBo;
import org.eiahe.hr.salary.domain.vo.SalaryHistoryVo;
import org.eiahe.hr.salary.mapper.SalaryHistoryMapper;
import org.eiahe.hr.salary.service.ISalaryHistoryService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 薪资历史Service业务层处理
 *
 * @author zhangnn
 */
@RequiredArgsConstructor
@Service
public class SalaryHistoryServiceImpl implements ISalaryHistoryService {

    private final SalaryHistoryMapper baseMapper;
    private final ISysDeptService deptService;

    @Override
    public SalaryHistoryVo queryById(Long historyId) {
        SalaryHistoryVo salaryHistoryVo = baseMapper.selectVoById(historyId);
        if(null != salaryHistoryVo.getDepartment()){
            SysDeptVo sysDeptVo = deptService.selectDeptById(salaryHistoryVo.getDepartment());
            if(Objects.nonNull(sysDeptVo)){
                salaryHistoryVo.setDepartmentName(sysDeptVo.getDeptName());
            }
        }
        return salaryHistoryVo;
    }

    @Override
    public List<SalaryHistoryVo> queryList(SalaryHistoryBo bo) {
        LambdaQueryWrapper<SalaryHistory> lqw = buildQueryWrapper(bo);
        List<SalaryHistoryVo> salaryHistoryVos = baseMapper.selectVoList(lqw);
        salaryHistoryVos.forEach(s->{
            if(null != s.getDepartment()){
                SysDeptVo sysDeptVo = deptService.selectDeptById(s.getDepartment());
                if(Objects.nonNull(sysDeptVo)){
                    s.setDepartmentName(sysDeptVo.getDeptName());
                }
            }
        });
        return salaryHistoryVos;
    }

    @Override
    public TableDataInfo<SalaryHistoryVo> queryPageList(SalaryHistoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SalaryHistory> lqw = buildQueryWrapper(bo);
        IPage<SalaryHistoryVo> page = baseMapper.selectVoPage(pageQuery.build(), lqw);
        page.getRecords().forEach(s->{
            if(null != s.getDepartment()){
                SysDeptVo sysDeptVo = deptService.selectDeptById(s.getDepartment());
                if(Objects.nonNull(sysDeptVo)){
                    s.setDepartmentName(sysDeptVo.getDeptName());
                }
            }
        });
        return TableDataInfo.build(page);
    }

    /**
     * 构建查询条件
     *
     * @param bo 薪资历史业务对象
     * @return 查询条件
     */
    private LambdaQueryWrapper<SalaryHistory> buildQueryWrapper(SalaryHistoryBo bo) {
        LambdaQueryWrapper<SalaryHistory> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getEmployeeNo()), SalaryHistory::getEmployeeNo, bo.getEmployeeNo());
        lqw.eq(StringUtils.isNotBlank(bo.getEmployeeName()), SalaryHistory::getEmployeeName, bo.getEmployeeName());
        lqw.eq(StringUtils.isNotBlank(bo.getYearMonth()), SalaryHistory::getYearMonth, bo.getYearMonth());
        lqw.eq(StringUtils.isNotBlank(bo.getRecordType()), SalaryHistory::getRecordType, bo.getRecordType());
        lqw.eq(StringUtils.isNotBlank(bo.getIdCard()), SalaryHistory::getIdCard, bo.getIdCard());
        lqw.eq(StringUtils.isNotBlank(bo.getDepartment()), SalaryHistory::getDepartment, bo.getDepartment());
        lqw.eq(StringUtils.isNotBlank(bo.getOperationType()), SalaryHistory::getOperationType, bo.getOperationType());
        lqw.orderByDesc(SalaryHistory::getCreateTime);
        return lqw;
    }

    @Override
    public Boolean insertByBo(SalaryHistoryBo bo) {
        SalaryHistory add = MapstructUtils.convert(bo, SalaryHistory.class);
        add.setOperationType("1");
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setHistoryId(add.getHistoryId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(SalaryHistoryBo bo) {
        bo.setOperationType("2");
        SalaryHistory update = MapstructUtils.convert(bo, SalaryHistory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SalaryHistory entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        List<SalaryHistory> salaryHistories = this.baseMapper.selectByIds(ids);
        for (SalaryHistory salaryHistory : salaryHistories) {
            salaryHistory.setOperationType("3");
            salaryHistory.setDelFlag("1");
            this.baseMapper.updateById(salaryHistory);
        }
        return true;
    }
}
